package ui;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class SelectDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private ResultSet rs;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SelectDialog(Shell parent, int style,ResultSet rs) {
		super(parent, style);
		setText("选择");
		this.rs = rs;
	}

	public void InitTable(){
		try {
			int i=0;
			while(rs.next()) {
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(0, rs.getString(1));
				item.setText(1, rs.getString(2));
				item.setText(2, rs.getString(3));
				item.setText(3, rs.getString(4));
				i++;
			}
			if(i==1){
				result = table.getItem(0).getText(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
			if(result!=null){
				shell.dispose();
				return result;
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(322, 183);
		shell.setText(getText());
		
		shell.setLocation(Display.getCurrent().getClientArea().width / 2 - shell.getShell().getSize().x/2, Display.getCurrent()
				.getClientArea().height / 2 - shell.getSize().y/2);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(table.getItems().length==0){
					shell.dispose();
					result="";
					return;
				}
				result = table.getSelection()[0].getText();
				shell.dispose();
			}
		});
		table.setBounds(10, 10, 293, 135);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(80);
		tableColumn.setText("\u7F16\u7801");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(80);
		tableColumn_1.setText("\u540D\u79F0");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(47);
		tableColumn_2.setText("\u7B80\u79F0");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(81);
		tableColumn_3.setText("\u884C\u4E1A");

		InitTable();
	}
}
